package com.asia5b.a5mall.middleware.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import androidx.annotation.IntDef;
import android.util.DisplayMetrics;
import android.util.Log;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;


/**
 * Created by zhangyuncai on 2018/11/7.
 * 语言管理
 */
public class LocalManageUtil {
    private static final String TAG = "LocalManageUtil";

    public static final int LANGUAGE_CN = 0;//默认中文
    public static final int LANGUAGE_EN = 1;//英文
    public static final int LANGUAGE_SU = 2;//印尼
    public static final int LANGUAGE_MS = 3;//马来西亚
    public static final int LANGUAGE_VI = 4;//越南
    public static final int LANGUAGE_TH = 5;//泰国

    @IntDef({LANGUAGE_CN, LANGUAGE_EN, LANGUAGE_SU, LANGUAGE_MS, LANGUAGE_VI, LANGUAGE_TH})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Language {
    }


    public static final String APP_LANGUAGE_SELECT = "APP_LANGUAGE_SELECT";//已经选择的语言
    public static final String APP_CURRENCY_SELECT = "APP_CURRENCY_SELECT3";//已经选择的货币
    public static final String APP_COUNTRY_SELECT = "APP_COUNTRY_SELECT";//已经选择的国家公馆

    private static LocalManageUtil instance;
    private Context mContext;

    public static void init(Context mContext) {
        if (instance == null) {
            synchronized (LocalManageUtil.class) {
                if (instance == null) {
                    instance = new LocalManageUtil(mContext);
                }
            }
        }
    }

    /**
     * @return en/cn
     */
    @Language
    public static int getAppLanguage() {
        Locale appLanguageSelect = LocalManageUtil.getAppLanguageSelect();
        if (appLanguageSelect == Locale.ENGLISH) {
            return LANGUAGE_EN;
        } else if (appLanguageSelect == new Locale("su")) {
            return LANGUAGE_SU;
        } else if (appLanguageSelect == new Locale("ms")) {
            return LANGUAGE_MS;
        } else if (appLanguageSelect == new Locale("vi")) {
            return LANGUAGE_VI;
        } else if (appLanguageSelect == new Locale("th")) {
            return LANGUAGE_TH;
        } else {
            return LANGUAGE_CN;
        }
    }

    public static LocalManageUtil getInstance() {
        if (instance == null) {
            throw new IllegalStateException("You must be initView MultiLanguageUtil first");
        }
        return instance;
    }

    private LocalManageUtil(Context context) {
        this.mContext = context;
    }


    public static Context attachBaseContext(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return createConfigurationResources(context);
        } else {
            LocalManageUtil.getInstance().setConfiguration();
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context createConfigurationResources(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = getInstance().getLanguageLocale();
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration);
    }

    /**
     * 设置语言
     */
    public void setConfiguration() {
        Locale targetLocale = getLanguageLocale();
        Configuration configuration = mContext.getResources().getConfiguration();
        configuration.setLocale(targetLocale);
        Resources resources = mContext.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!
    }


    /**
     * 获取设置的语言
     * 0 系统默认
     *
     * @return
     */
    private Locale getLanguageLocale() {
        return getAppLanguageSelect();
    }


    /**
     * 获取设置的语言
     * 0 系统默认
     *
     * @return
     */
    private static Locale getAppLanguageSelect() {
        int appLanguageSelect = ExtendMethodsKt.getAppLanguageSelect();
        Log.d(TAG, "--->>>>>>>>---select.appLanguageSelect:" + appLanguageSelect);
        switch (appLanguageSelect) {
            case LANGUAGE_EN:
                return Locale.ENGLISH;
            case LANGUAGE_MS:
                return new Locale("ms");
            case LANGUAGE_VI:
                return new Locale("vi");
            case LANGUAGE_TH:
                return new Locale("th");
            case LANGUAGE_SU:
                return new Locale("su");
            default:
                return Locale.CHINA;
        }
    }

    /**
     * 获取手机的语言
     *
     * @return
     */
    public static Locale getSysLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }

    /**
     * 设置选择的语言
     *
     * @param language 0 系统默认
     */
    public static void setAppLanguageSelect(@Language int language) {
        ExtendMethodsKt.setAppLanguageSelect(language);
    }

}
